/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[TestServletContext.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package servlet.api;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

/**
 * javax.servlet.ServletContext
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
@WebServlet(name = "TestServletContext", urlPatterns = "/servletContext")
public class TestServletContext extends GenericServlet {

	private static final long serialVersionUID = 3619200455369182031L;
	private ServletConfig config = null;

	@Override
	public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}

	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		// 获得ServletContext对象
		ServletContext context = config.getServletContext();

		// 获得<context-param>配置的初始化参数
		Enumeration<String> contextParamNames = context.getInitParameterNames();
		while (contextParamNames.hasMoreElements()) {
			String contextParamName = contextParamNames.nextElement();
			String contextParamValue = context.getInitParameter(contextParamName);
			System.out.println(contextParamName + "=" + contextParamValue);
		}

		// 获取文件的绝对路径, 路径为当前Web应用的根目录[WebContent]
		String realPath = context.getRealPath("/images/1.jpg");
		System.out.println("image path = " + realPath);

		// 获取当前Web应用的名称
		String appName = context.getContextPath();
		System.out.println("appName = " + appName);

		// 一个获得文件名的写法
		String path = context.getRealPath("/images/1.jpg");
		String fileName = path.substring(path.lastIndexOf("\\") + 1);
		System.out.println(fileName);

		this.readResources(context);
	}

	/**
	 * 读取资源文件的几种方法
	 * 
	 * @param context
	 * @throws IOException
	 */
	private void readResources(ServletContext context) throws IOException {
		// 使用ServletContext获取资源的输入流, 路径为当前Web应用的根目录[WebContent]
		InputStream in = context.getResourceAsStream("/images/1.jpg");
		System.out.println(in.available());

		// 使用ServletContext获取位于src目录内的资源
		in = context.getResourceAsStream("/WEB-INF/classes/servlet/api/c3p0.properties");
		System.out.println(in.available());

		// 使用ClassLoader获取位于src目录内的资源
		in = this.getClass().getClassLoader().getResourceAsStream("servlet/api/c3p0.properties");
		System.out.println(in.available());

		/**
		 * classLoader的根路径位于src目录下, 如果要读取的资源在包中, 则只需加上包路径<br/>
		 * <code>
		 * classLoader.getResourceAsStream("servlet/api/c3p0.properties");
		 * </code>
		 * 
		 * servletContext的根路径位于WebContent目录下, 如果要读取放置在src目录下的资源,
		 * 需要从WEB-INF/classes中读取<br/>
		 * <code>
		 * servletContext.getResourceAsStream("/WEB-INF/classes/servlet/api/c3p0.properties");
		 * </code>
		 * 
		 * classLoader不适合加载大文件, 会导致JVM内存溢出<br/>
		 */
	}
}
